 /**
This file is part of Studentport.

    Studentport is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Studentport is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Studentport.  If not, see <http://www.gnu.org/licenses/>.

**/
package com.groupalpha.studentport;

import java.util.LinkedList;
import java.util.List;

import RSS.RSSItem;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.groupalpha.helpers.DOMPARSER;
import com.groupalpha.helpers.MyArrayAdapter;
import com.httpclasses.HtmlHelper;

/**
 * 
 * Will present the user with an webview loaded with htmlstring fetched from HtmlHelper.behandlaGasque() method.
 * @author GroupAlpha
 *
 */
public class GasqueEN extends Activity {
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gasque);
        final Button logoffButton = (Button) findViewById(R.id.button2);
        final LinkedList<RSSItem> items = new LinkedList<RSSItem>();
        logoffButton.setBackgroundResource(R.drawable.logoff_button_en);
        final ListView listview = (ListView)findViewById(R.id.listView1);
       // final WebView browser = (WebView) findViewById(R.id.gasqueView);
		
		 final ProgressDialog dialog = new ProgressDialog(GasqueEN.this);
	        final Toast toast = Toast.makeText(GasqueEN.this, "Error", Toast.LENGTH_SHORT);
	        final Toast info = Toast.makeText(getApplicationContext(), "Gasquen is not available in english, sorry", Toast.LENGTH_LONG);
		        dialog.setTitle("Loading Gasque");
		   	 	dialog.setMessage("Wait...");
		   	 	dialog.setCancelable(false);
		   	 	dialog.show();
		   	 new Thread() {
					public void run() {
						try {
							// browser.loadData(HtmlHelper.behandlaGasque(),
							// "text/html", "UTF-8");
							List<RSSItem> l = DOMPARSER.getGasqueFromXML();
							if (items.isEmpty())
								items.addAll(l);
							else {
								for (int i = 0; i < items.size(); i++)
									items.remove(i);
								items.addAll(l);
							}
							runOnUiThread(new Runnable(){

								@Override
								public void run() {
									listview.setAdapter(new MyArrayAdapter(GasqueEN.this, R.layout.gasque, items));
									
								}
								
								
							});
							
							
							dialog.dismiss();
						} catch (Exception e) {
							// TODO Auto-generated catch block
							toast.show();
							dialog.dismiss();
						}

					}
				}.start();
				
				info.show();
			
		   	 	logoffButton.setOnClickListener(new View.OnClickListener() {
		            public void onClick(View v) {
		            	Intent intent = new Intent(GasqueEN.this, StudentportActivity.class); 
						intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Removes other Activities from stack 
						startActivity(intent);
		            }
		            });
			
	
    }

   
}